<?php
// based url
$url ="/cambodiajob/";
// get website header that contains logo and menus
function getHeader()
{
	global $url;
	$str = $url."index.php";
	$headertag ="<div class='row-fluid row-header'>
	<div class='span3 text-center'><a href='$str'><img src='{$url}img/logo_kh.png' alt='Khschools.com' /></a></div>
	<div class='span9'>".getTopMenu()."</div></div>";
	echo $headertag;
}

// get locations and its jobs
function getLocation(){
	$sql ="select * from location order by locationname";
	$result = runQuery($sql);
	$n = mysqli_num_rows($result);
	$items =(int)$n/4;
	// convert result to array
	$rows;
	while($row=mysqli_fetch_array($result))
	{
			$rows[] = "<li><a href='view.php?locationid=".$row[0]."&locationname=".$row[1]."'>". $row[1]. "</a> <span style='color:red'>(".getNumberOfJob_l($row[0]).")</span></li>";
	}
	$i=0;
	if($n>0)
	{
		echo "<div class='span3'>";
		echo "<ul>";
			while($i<$items)
			{
				echo $rows[$i];
				$i++;
			}
		echo "</ul>";
		echo "</div>";
		echo "<div class='span3'>";
		echo "<ul>";
			while($i<$items*2)
			{
				echo $rows[$i];
				$i++;
			}
		echo "</ul>";
		echo "</div>";
		echo "<div class='span3'>";
		echo "<ul>";
			while($i<$items*3)
			{
				echo $rows[$i];
				$i++;
			}
		echo "</ul>";
		echo "</div>";
		echo "<div class='span3'>";
		echo "<ul>";
			while($i<$n)
			{
				echo $rows[$i];
				$i++;
			}
		echo "</ul>";
		echo "</div>";
	}
}

// get categories and its jobs
function getCategory(){
	$sql ="select * from category order by catname";
	$result = runQuery($sql);
	$n = mysqli_num_rows($result);
	$items =(int)$n/4;
	// convert result to array
	$rows;
	while($row=mysqli_fetch_array($result))
	{
			$rows[] = "<li><a href='view.php?catid=".$row[0]."&catname=".$row[1]."'>". $row[1]. "</a> <span style='color:red'>(".getNumberOfJob($row[0]).")</span></li>";
	}
	$i=0;
	if($n>0)
	{
		echo "<div class='span3'>";
		echo "<ul>";
			while($i<$items)
			{
				echo $rows[$i];
				$i++;
			}
		echo "</ul>";
		echo "</div>";
		echo "<div class='span3'>";
		echo "<ul>";
			while($i<$items*2)
			{
				echo $rows[$i];
				$i++;
			}
		echo "</ul>";
		echo "</div>";
		echo "<div class='span3'>";
		echo "<ul>";
			while($i<$items*3)
			{
				echo $rows[$i];
				$i++;
			}
		echo "</ul>";
		echo "</div>";
		echo "<div class='span3'>";
		echo "<ul>";
			while($i<$n)
			{
				echo $rows[$i];
				$i++;
			}
		echo "</ul>";
		echo "</div>";
	}
}

// print search job form
function getSearchForm(){
	$tags="<form method='post'><input type='text'/><input type='password'/><br/><input type='button' value='Search'/></form>";
	echo $tags;
}

// get the number of jobs in each category
function getNumberOfJob($catid){
	$sql="select * from job where catid=$catid";
	$result = runQuery($sql);
	return mysqli_num_rows($result);
}

// get the number of jobs in each location
function getNumberOfJob_l($l)
{
	$sql="select * from job where locationid=$l";
	$result = runQuery($sql);
	return mysqli_num_rows($result);
}

// execute select statement
function runQuery($sql){
	$con = mysqli_connect("localhost","root","","cambojob");
	$result = mysqli_query($con, $sql);
	mysqli_close($con);
	return $result;
}
// execute delete and update
function runNonQuery($sql){
	$con = mysqli_connect("localhost","root","","cambojob");
	$st = mysqli_prepare($con,$sql);
	$i=0;
	if(mysqli_stmt_execute($st)){
		$i=1;
	}
	@mysqli_stmt_close($st);
	return $i;
}
// get all job in a category
function getJobByCategory($catid){
	$sql ="select * from job where catid=$catid";
	$result = runQuery($sql);
	if(mysqli_num_rows($result)>0)
	{
		echo "<table class='table'>";
		echo "<tr><th>Job ID</th><th>Job Title</th><th>Description</th></tr>";
		while($row=mysqli_fetch_array($result))
		{
			echo "<tr>";
			echo "<td>".$row[0]."</td><td>".$row[1]."</td><td>".$row[2]."</td>";
			echo "</tr>";
		}
		echo "</table>";
	}
	else
		echo "No Job Found!";
}

// get all job in alocation
function getJobByLocation($locationid)
{
	$sql ="select * from job where locationid=$locationid";
	$result = runQuery($sql);
	if(mysqli_num_rows($result)>0)
	{
		echo "<table class='table'>";
		echo "<tr><th>Job ID</th><th>Job Title</th><th>Description</th></tr>";
		while($row=mysqli_fetch_array($result))
		{
			echo "<tr>";
			echo "<td>".$row[0]."</td><td>".$row[1]."</td><td>".$row[2]."</td>";
			echo "</tr>";
		}
		echo "</table>";
	}
	else
		echo "No Job Found!";
}

// return top ment for getHeader function
function getTopMenu()
{
	global $url;
	$sql="select * from topmenu where isdeleted=0 order by position";
	$result = runQuery($sql);
	$menu="";
	if(mysqli_num_rows($result)>0)
	{
		$menu = "<ul class='nav nav-pills pull-right' id='nav'>";
		while($row=mysqli_fetch_array($result))
		{
			$strurl=$url.$row[2];
			$menu .="<li><a href='$strurl' class='text-white'>$row[1]</a></li>";
		}
		$menu .="</ul>";
	}
	return $menu;
}

// validate job provider information

function validateEmployerInfo($info)
{
	$name = $info[0];
	$position = $info[1];
	$comname = $info[2];
	$email = $info[3];
	$pass = $info[4];
	$repass = $info[5];
	if(strlen($name)<5 || strpos(trim($name), ' ')!=true)
	{
		return "Invalid full name!";	
	}
	elseif(strlen($position)<4+1)
	{
		return "Invalid position!";
	}
	elseif(strlen($comname)<5)
	{
		return "Invalid company name!";
	}
	elseif(!filter_var($email,FILTER_VALIDATE_EMAIL))
	{
		return "Invalid email address!";
	}
	elseif(strlen($pass)<3 || strlen($pass)>12)
	{
		return "Invalid password!";
		
	}
	elseif($pass!=$repass)
	{
		return "Mismatched password!";
	}
	else 
	{
		return "OK";
	}
}

// insert new employer
function addEmployer($info){
	$sql="insert into employer(fullname,position,comid,email,pass) values(?,?,?,?,?)";
	$val=false;
	$fname = $info[0];
	$pos = $info[1];
	$comid=$info[2];
	$email = $info[3];
	$pass = md5($info[4]);
	$con = mysqli_connect("localhost","root","","cambojob");
	if(!$con)
	{
		$val=false;
	}
	else{
		$rows = mysqli_query($con,"select * from employer where email ='". $email . "'");
		$st = mysqli_prepare($con,$sql);
		if(!mysqli_stmt_bind_param($st, "ssiss",$fname,$pos,$comid,$email,$pass))
		{
			$val=false;
		}
		else{
			if(mysqli_num_rows($rows)<1){
				if(mysqli_stmt_execute($st))
				{
					$val=true;
				}
				else
				{
					$val=false;
				}
			}
		}
		@mysqli_stmt_close($st);
	}
	@mysqli_close($con);
	return $val;
}

// function to login as employer or employee
function loginAs($email,$pass,$as)
{
	$val="";
	if($as==1){
		$result = runQuery("select id, email,pass from employer where email='" . $email . "'");
		$row= array("","","");
		if(@mysqli_num_rows($result)==1)
		{
			$row = mysqli_fetch_array($result);
		}
		if($row[1]==$email)
		{
			if($row[2]==md5($pass))
			{
				$val="OK";
				$_SESSION["userid"]=$row[0];
				$_SESSION["email"]=$row[1];
				$_SESSION["loginas"]=1;
				$_SESSION['pass'] = $pass;
			}
			else {
					
				$val ="Invalid email or passowrd!";
			}
		}
		else{
			$val ="Invalid email or passowrd!";
		}
	}
	else{
		
		// read user name or password from employee...
	}
	
	return $val;
}
// get jobs by employer id

function getJobByEmployer($empId){
	
	$sql = "select * from job where posterid=".$empId;
	
	$result = runQuery($sql);
	return $result;
}
function getJobDetail($jobid)
{
	$sql ="select * from job where jobid=".$jobid;
	$result=runQuery($sql);
	return $result;
}
?>


